//Copyright (C) 2009 Ron Coleman. Contact: ronncoleman@gmail.com
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 3 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

package org.workplicity.android.cia.ui.elogfragments;

import java.util.Calendar;
import java.util.Date;

import org.workplicity.android.cia.R;
import org.workplicity.android.cia.ui.ElogEditActivity;
import org.workplicity.android.cia.ui.MainActivity;

import android.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

/**
 * 
 * @author brian.gormanly
 *
 */

public class LogEditFragment extends Fragment {
	
	// set up controls
	DatePicker dp;
	TimePicker tp;
	EditText description;
	EditText workDone;
	
	
	// container for the fragment
	private View mContentView;
	
	// get the parent activity
	ElogEditActivity myActivity;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		
		mContentView = inflater.inflate(R.layout.log_edit_fragment, null);
        //final ImageView imageView = (ImageView) mContentView.findViewById(R.id.image);
		mContentView.setDrawingCacheEnabled(false);
		
		// initialize the local ref to the parent activity
        myActivity = (ElogEditActivity)getActivity();
		
		// initialize the controls
		dp = (DatePicker)mContentView.findViewById(R.id.datePicker1);
		tp = (TimePicker)mContentView.findViewById(R.id.timePicker1);
		description = (EditText)mContentView.findViewById(R.id.logEditTabJobDescription);
		workDone = (EditText)mContentView.findViewById(R.id.logEditTabWorkDone);
		
		// check to see if this is an edit and disallow changing the date.
		if(!myActivity.getApp().isCreateMode()) {
			dp.setClickable(false);
			tp.setClickable(false);
			description.setKeyListener(null);
			workDone.setKeyListener(null);
		}
		else {
			dp.setClickable(true);
			tp.setClickable(true);
			
		}
		
		description.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

            }

            public void afterTextChanged(Editable s) {
            	// set the dirty flag
            	myActivity.getApp().setCurrentElogDirty(true);
            	
            	// update the current elog with the edited description
            	myActivity.getApp().getCurrentElog().setDescription(description.getText().toString());
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
 
            }
        });
		
		workDone.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

            }

            public void afterTextChanged(Editable s) {
		        	// set the dirty flag
		        	myActivity.getApp().setCurrentElogDirty(true);
		        	
		        	// update the current elog with the edited description
		        	myActivity.getApp().getCurrentElog().setWorkDone(workDone.getText().toString());
            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
 
            }
        });


		
		refresh();
		
		return mContentView;
	}
	
	public void refresh() {
		// refresh the fragment check first to see if there is an elog being edited
		if(myActivity.getApp().getCurrentElog() != null) {
			
			Calendar cal = Calendar.getInstance();

	        cal.setTimeInMillis(myActivity.getApp().getCurrentElog().getCreateDate().getTime());
	        int mon = cal.get(Calendar.MONTH);
	        int day = cal.get(Calendar.DAY_OF_MONTH);
	        int year = cal.get(Calendar.YEAR);
	        
	        int hr = cal.get(Calendar.HOUR_OF_DAY);
	        int min = cal.get(Calendar.MINUTE);
	        String amPm = cal.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM";
			
			dp.updateDate(year, mon, day);
			
			tp.setCurrentHour(hr);
			tp.setCurrentMinute(min);
			
			description.setText(myActivity.getApp().getCurrentElog().getDescription().toString());
			workDone.setText(myActivity.getApp().getCurrentElog().getWorkDone().toString());
			
			
		}
		
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
    
    public void onItemSelected(AdapterView parent, View v, int position, long id) { 
		// this will do something
    	
	}

}
